/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.helpers.manto;

import com.uca.sisbec.beans.CodeAndName;
import com.uca.sisbec.forms.manto.RolForm;
import com.uca.sisbec.mapping.ModuloXRol;
import com.uca.sisbec.mapping.ModuloXRolId;
import com.uca.sisbec.mapping.Rol;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Juank
 */
public class RolHelper {
    private Session session;
    
    public RolHelper(Session session){
        this.session=session;
    }

    public List<CodeAndName> getRolesList() {
        String hql = "select new com.uca.sisbec.beans.CodeAndName "
                + "(idRol, rol) from Rol";
        Query query = session.createQuery(hql);
        return new ArrayList<CodeAndName>(query.list());
    }

    public void setRolForm(RolForm rf) {
        Rol rol = getRol(rf.getRoles());
        rf.setCodigo(rol.getIdRol());
        rf.setNombre(rol.getRol());
        rf.setDescripcion(rol.getDescripcion());
    }
    
    public Rol getRol(int codigo){
        Rol rol = (Rol) session.get(Rol.class, codigo);
        return rol;
    }

    public void modificarRol(RolForm rf) {
        Rol rol = getRol(rf.getCodigo());
        rol.setDescripcion(rf.getDescripcion());
        rol.setRol(rf.getNombre());
        session.update(rol);
        Query query = session.createQuery("delete from ModuloXRol where id.idRol = :v_rol");
        query.setInteger("v_rol", rol.getIdRol());
        query.executeUpdate();
        /*Query query = session.createQuery("delete from ModuloXRol where id.idRol = :v_rol");
        query.setInteger("v_rol", rol.getIdRol());
        query.executeUpdate();
        ModuloXRol mxr;
        ModuloXRolId id;
        for (int i = 0; i < rf.getSeleccionadas().length; i++){
            id = new ModuloXRolId(rf.getSeleccionadas()[i], rol.getIdRol());
            mxr = new ModuloXRol();
            mxr.setId(id);
            session.save(mxr);
        }*/
    }
    
    public void agregarModulos(RolForm rf){
        ModuloXRol mxr;
        ModuloXRolId id;
        if (rf.getSeleccionadas() == null || rf.getSeleccionadas().length == 0)
            return;
        for (int i = 0; i < rf.getSeleccionadas().length; i++){
            id = new ModuloXRolId(rf.getSeleccionadas()[i], rf.getCodigo());
            mxr = new ModuloXRol();
            mxr.setId(id);
            session.save(mxr);
        }
    }

    public void insertarRol(RolForm rf) {
        Rol rol = new Rol();
        rol.setDescripcion(rf.getDescripcion());
        rol.setRol(rf.getNombre());
        session.save(rol);
        rf.setCodigo(rol.getIdRol());
    }

    public List<CodeAndName> getAvailableModulos(int codigo) {
        String hql = "select new com.uca.sisbec.beans.CodeAndName(m.idModulo, m.modulo) "
                + "from Modulo m "
                + "where m.idModulo not in "
                + "(select mr.id.idModulo from ModuloXRol mr "
                + "where mr.id.idRol = :v_rol) "
                + "and m.opcPrincipal is not null "
                + "order by m.opcPrincipal, m.idModulo";
        Query query = session.createQuery(hql);
        query.setInteger("v_rol", codigo);
        return new ArrayList<CodeAndName>(query.list());
    }

    public List<CodeAndName> getSelectedModulos(int codigo) {
        String hql = "select new com.uca.sisbec.beans.CodeAndName(m.idModulo, m.modulo) "
                + "from Modulo m "
                + "where m.idModulo in "
                + "(select mr.id.idModulo from ModuloXRol mr "
                + "where mr.id.idRol = :v_rol) "
                + "and m.opcPrincipal is not null "
                + "order by m.opcPrincipal, m.idModulo";
        Query query = session.createQuery(hql);
        query.setInteger("v_rol", codigo);
        return new ArrayList<CodeAndName>(query.list());
    }
}
